package com.springboot.autoconfigure.oss.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.PathUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.BucketInfo;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.PutObjectRequest;
import com.springboot.autoconfigure.oss.config.AliyunOSSPropertis;
import com.springboot.autoconfigure.oss.config.LocalConfigPropertis;
import com.springboot.autoconfigure.oss.config.OssConfigPropertis;
import com.springboot.autoconfigure.oss.model.FileInfo;
import com.springboot.autoconfigure.oss.model.FileOssOperationStatus;
import com.springboot.autoconfigure.oss.model.FileUploadStatus;
import com.springboot.autoconfigure.oss.service.AbstractInitClientServer;
import com.springboot.autoconfigure.oss.service.AbstractLocalFilePathHelper;
import com.springboot.autoconfigure.oss.service.FileNameService;
import com.springboot.autoconfigure.oss.service.OssApiService;
import com.springboot.autoconfigure.oss.util.OSSFileHelperUtil;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.file.PathUtils;
import org.springframework.beans.factory.InitializingBean;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.*;


/**
 * 本地文件上传  nginx 代理路径
 */
public class LocalApiServiceImpl implements OssApiService<LocalConfigPropertis>, AbstractInitClientServer<OSS>,
        InitializingBean {
    //文件名层级分隔符号
    private static final String SPLIT_PATH="_";

    //URL 上传指定文件名前缀
    private static final String URL_UPLOAD_PREFIX="UPLOAD-BY-URL";
    private FileNameService fileNameService;
    private AbstractLocalFilePathHelper abstractLocalFilePathHelper;

    private LocalConfigPropertis localConfigPropertis;
    private OssConfigPropertis ossConfigPropertis;



    public LocalApiServiceImpl(LocalConfigPropertis localConfigPropertis, OssConfigPropertis ossConfigPropertis) {
        this.localConfigPropertis = localConfigPropertis;
        this.ossConfigPropertis = ossConfigPropertis;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.fileNameService  = SpringUtil.getBean(FileNameService.class);
        this.abstractLocalFilePathHelper  = SpringUtil.getBean(AbstractLocalFilePathHelper.class);
        String localDir = localConfigPropertis.getLocalDir();
        new File(localDir).mkdirs();
        new File(localConfigPropertis.getLocalDir()+fileNameService.getFilePath()).mkdirs();
    }

    @Override
    public OSS getinit(){
       return null;
    }

    @Override
    public FileUploadStatus uploadFile(URL url) {
        return null;
    }

    @Override
    public FileUploadStatus uploadFile(URL url,String filePrix){
        return null;
    }

    @Override
    public FileUploadStatus uploadFile(File file) {
        if (file==null){
            FileUploadStatus fail = FileUploadStatus.fail();
            fail.setErrorMsg("文件不能为空");
            return fail;
        }
        /**
         * 本地保存
         */
        String filePath = fileNameService.getFilePath();
        File fileDir = new File(localConfigPropertis.getLocalDir() + filePath);
        fileDir.mkdirs();
        File localFile = new File(fileDir, fileNameService.getFileName(file));
        FileUtil.copy(file,localFile,true);
        FileUploadStatus suceess = FileUploadStatus.suceess();
        // TODO 路径问题
        suceess.setUrl(abstractLocalFilePathHelper.getFileUrlPath(localConfigPropertis,localFile,localFile.getName()));
        Map<String, Object> fileOtherInfo = OSSFileHelperUtil.getFileOtherInfo(file);
        fileOtherInfo.put("filePath",filePath);
        fileOtherInfo.put("file",localFile);
        suceess.setOtherInfo(fileOtherInfo);
        return suceess;
    }


    @Override
    public List<FileInfo> getBucketSpaceFile(String prefix, int limit) {
        return null;
    }

    @Override
    public FileUploadStatus uploadAbsoulteFilePath(File file, String filePath) {
        FileUploadStatus suceess = FileUploadStatus.suceess();
        return suceess;
    }

    @Override
    public FileUploadStatus uploadFile(File file, String fileName) {
        return null;
    }

    @Override
    public FileUploadStatus uploadFileAndCallBack(File file, String callbackUrl) {
        return null;
    }

    @Override
    public List<String> findFile(String... fileNames) {
        // TODO
      return  null;
    }


    @Override
    public FileUploadStatus findFileByKey(String fileKey) {
        return null;
    }

    @Override
    public FileOssOperationStatus delFile(String... fileNames) {
        return null;
    }


}
